import { createRouter, createWebHistory } from 'vue-router';
import Login from '../components/Login.vue';
import Register from '../components/Register.vue';
import Dashboard from '../components/Dashboard.vue';
import Chat from '../components/Chat.vue';
import Home from '../components/Home.vue';
import GoodsTable from "@/components/GoodsTable";

const routes = [
  { path: '/login', name: 'Login', component: Login },
  { path: '/register', name: 'Register', component: Register },
  { path: '/dashboard', name: 'Dashboard', component: Dashboard },
  { path: '/chat', name: 'Chat', component: Chat },
  {path: '/home', name: 'Home', component: Home },
  {path: '/', redirect: '/login' },
  {path:'/api/goods',name:'GoodsTable',component: GoodsTable}
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (to.name !== 'Login' && to.name !== 'Register' && !token) {
    next({ name: 'Login' });
  } else {
    next();
  }
});

export default router;
